* regressions_formal_bite_hora.do JEP	01/24/15
* Copy of  regressions_tpaper_formal.do, but integrated in the project
* Does regressions that are in the term paper, for formal workers: RIF4

*************************** Change log ********************************** 

/*
	01/25/15	Add code to be able to run this file as stand-alone without the project commands interfering
				Add code for all quantiles
				Restrict this dofile to rif4, formal only
	01/26/15	Add confirmation of .ster files to avoid reruns
	01/27/15	Add creation of ster folder
	07/13/15	Rely on term paper version of file
*/	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	loc master "/home/jperez/AA_Minimum_Wage" 
 loc pr = 0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")
	project, relies_on("`master'/Code/Est/export_rif.do")
	project, relies_on("`master'/Code/Est/confirmedrun.do")
	
	project, relies_on("`master'/Code/Est/regressions_tpaper_formal.do")
}
*/


* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"
do "`master'/Code/Est/export_rif.do"
do "`master'/Code/Est/confirmedrun.do"


use "`master'/Data/Source/ocupados_mw.dta", clear

* Choose quantiles
glo qtiles "5 10 15 20 25 30 50 75 90"
* glo qtiles "90"
* Number of bootstraps
glo boot=10

* RIF 4 - Tables numbered 4 in term paper

* Choose sample
mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal
tab rif4sample
tab tipo_trabajador if rif4sample
tab year if rif4sample
tab horas_semana if rif4sample 
tab sex if rif4sample
tab afiliado_salud if rif4sample
* Build mw measures
mw_measures rif4sample , stub(rif4)
* Build treatment
mytreatment, posts("tq(1999q1)") mw(bite_rif4) wage(salario_hora_real)

* RIF regressions

* Create estimation results folder if it doesn't already exist
cap mkdir "`master'/Ster"
cap mkdir "`master'/Ster/rif4_new"

* I found that these do not replicate what was initially in the paper exactly, because there were 132 observations that were being mistakenly included. Coefs are pretty close anyway.

foreach x in $qtiles {
	di " Percentile `x'"
	loc fw: word 1 of $qtiles
	loc count: word count $qtiles
	di `count'
	loc lw: word `count' of $qtiles
	if `x'==`lw' & `pr'!=0 loc reg="register"
	else loc reg ""
	if `x'==`fw' loc r="replace"
	else loc r="append"
	* Panel A. Employment
	confirmedrun, `reg' controls() name(`master'/Ster/rif4_new/reg_emp_tq(1999q1)_bite_salario_hora_real_50_edom) rif(rif4A) p(`x') r(`r') sample(rif4sample)
	* cgmrif2 lw treat i.ccode i.time emp if rif4sample , hyp(0) boot($boot) cluster(ccode) x(treat) i(ccode) t(time) q(`x') rif(rif4A`x')
	* exportrif, name(`master'/Ster/rif4_new/reg_emp_tq(1999q1)_bite_salario_hora_real_50_edom) p(`x') r(`r')
	* Panel B. Employment and Bartik Price Variable
	confirmedrun, `reg' controls(bartikp1) name(`master'/Ster/rif4_new/reg_emp_bp_tq(1999q1)_bite_salario_hora_real_50_edom) rif(rif4B) p(`x') r(`r') sample(rif4sample)
	* Panel C. Employment and Bartik Quantity Variable
	confirmedrun, `reg' controls(bartikq) name(`master'/Ster/rif4_new/reg_emp_bq_tq(1999q1)_bite_salario_hora_real_50_edom) rif(rif4C) p(`x') r(`r') sample(rif4sample)
	* Panel D. Employment and City Trends
	confirmedrun, `reg' controls(i.ccode#c.trend) name(`master'/Ster/rif4_new/reg_sst_emp_tq(1999q1)_bite_salario_hora_real_50_edom) rif(rif4D) p(`x') r(`r') sample(rif4sample)
	* Panel E. Employment, Bartik Price Variable and City Trends
	confirmedrun, `reg' controls(i.ccode#c.trend bartikp1) name(`master'/Ster/rif4_new/reg_sst_emp_bp_tq(1999q1)_bite_salario_hora_real_50_edom) rif(rif4E) p(`x') r(`r') sample(rif4sample)
	* Panel F. Employment, Bartik Quantity Variable and City Trends
	confirmedrun, `reg' controls(i.ccode#c.trend bartikq) name(`master'/Ster/rif4_new/reg_sst_emp_bq_tq(1999q1)_bite_salario_hora_real_50_edom) rif(rif4F) p(`x') r(`r') sample(rif4sample)	
}
